package util
{
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	
	import mx.controls.Alert;
	import mx.rpc.http.HTTPService;

	public class downLoad
	{
		private var fileRef: FileReference=new FileReference();
		public function downLoad()
		{
			
		}
		public function downloadAttach(item:Object,url:String):void{  
			initFile();
			var urlReq:String=url+encodeURIComponent(item.模板名称);
			fileRef.download(new URLRequest(urlReq));  
		} 
		private function initFile():void{  
			fileRef = new FileReference();  
			
			fileRef.addEventListener(Event.CANCEL, doEvent);  
			fileRef.addEventListener(Event.COMPLETE, completeHandler);  
			fileRef.addEventListener(Event.OPEN, doEvent);  
			fileRef.addEventListener(Event.SELECT, doEvent);  
			fileRef.addEventListener(HTTPStatusEvent.HTTP_STATUS, doEvent);  
			fileRef.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);  
			fileRef.addEventListener(ProgressEvent.PROGRESS, processEvent);  
			fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doEvent);  
		}  
		private function doEvent(evt:Event):void {  
			// 取得当前FileReference的引用  
			var fr:FileReference = evt.currentTarget as FileReference;  
			trace("evt Type:"+evt.type+"evt"+evt.toString());  
		}  
		private function processEvent(event:ProgressEvent):void{  
//			dl_cancel.visible=true;  
//			mydlBar.maximum=event.bytesTotal;  
//			mydlBar.setProgress(event.bytesLoaded,event.bytesTotal);   
//			mydlBar.label="已下载："+Math.round((event.bytesLoaded * 100 /event.bytesTotal))+"%";  
		}  
		private function cancelHandler():void{  
			this.fileRef.cancel();  
			//dl_cancel.visible=false;  
		}  
		private function completeHandler(event:Event):void{  
			//mydlBar.label="已完成";  
			//dl_cancel.visible=false;  
		}  
		private function ioErrorHandler(event:IOErrorEvent):void{  
			Alert.show(event.text);  
		}  
	}
}